package com.zn.kjj.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: DateUtil
 * @Description: 时间工具类
 * @author w15579
 * @date. 2016年12月15日
 * @version v1.0.9
 */
public class DateUtil {
    /**
     * yyyy-MM-dd
     */
    public static final String CN_DATE_FORMAT = "yyyy-MM-dd";
    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public static final String CN_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * 获取当前时间
     * @return Date 当前时间
     */
    public static Date now() {
        return new Date();
    }

    /**
     * 获取指定格式的时间对象
     * @param strDate
     *            时间字符串
     * @param fromFormat
     *            时间格式
     * @return Date date对象
     */
    public static Date getDate(String strDate, String fromFormat) {
        DateFormat f = new SimpleDateFormat(fromFormat);
        Date date = null;
        try {
            date = f.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 获取指定格式的时间字符串对象
     * @param date
     *            时间字符串
     * @param fromFormat
     *            时间格式
     * @return String 时间字符串
     */
    public static String getStringDate(Date date, String fromFormat) {
        DateFormat f = new SimpleDateFormat(fromFormat);
        return f.format(date);
    }

    /**
     * 获取时间段内的日期集合
     * @param beginTime
     *            开始时间
     * @param endTime
     *            结束时间
     * @return 时间集合
     */
    public static List<String> getTimeList(String beginTime, String endTime) {
        List<String> timeList = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            // timeList.add(beginTime);
            Calendar calBegin = Calendar.getInstance();
            calBegin.setTime(sdf.parse(beginTime));
            timeList.add(sdf.format(calBegin.getTime()));
            Calendar calEnd = Calendar.getInstance();
            calEnd.setTime(sdf.parse(endTime));
            while (sdf.parse(endTime).after(calBegin.getTime())) {
                calBegin.add(Calendar.DAY_OF_MONTH, 1);
                timeList.add(sdf.format(calBegin.getTime()));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timeList;
    }

    /**
     * 获取时间段内的月份集合
     * @param beginTime
     *            开始时间
     * @param endTime
     *            结束时间
     * @return 时间集合
     */
    public static List<String> getMonthList(String beginTime, String endTime) {
        List<String> timeList = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        try {
            // timeList.add(beginTime);
            Calendar calBegin = Calendar.getInstance();
            calBegin.setTime(sdf.parse(beginTime));
            timeList.add(sdf.format(calBegin.getTime()));
            Calendar calEnd = Calendar.getInstance();
            calEnd.setTime(sdf.parse(endTime));
            while (sdf.parse(endTime).after(calBegin.getTime())) {
                calBegin.add(Calendar.MONTH, 1);
                timeList.add(sdf.format(calBegin.getTime()));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timeList;
    }

    /**
     * endTime比beginTime多的天数
     * @param beginTime 开始
     * @param endTime 结束
     * @return 是否超过区间
     */
    public static int differentDays(String beginTime, String endTime) {
        Calendar cal1 = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            cal1.setTime(sdf.parse(beginTime));

            Calendar cal2 = Calendar.getInstance();
            cal2.setTime(sdf.parse(endTime));
            int day1 = cal1.get(Calendar.DAY_OF_YEAR);
            int day2 = cal2.get(Calendar.DAY_OF_YEAR);

            int year1 = cal1.get(Calendar.YEAR);
            int year2 = cal2.get(Calendar.YEAR);
            if (year1 != year2) {// 同一年
                int timeDistance = 0;
                for (int i = year1; i < year2; i++) {
                    if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {// 闰年
                        timeDistance += 366;
                    } else {// 不是闰年
                        timeDistance += 365;
                    }
                }
                return timeDistance + (day2 - day1);
            } else {// 不同年
                return day2 - day1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * endTime比beginTime差几个月
     * @param beginTime 开始
     * @param endTime 结束
     * @return 大小
     */
    public static int getMonth(String beginTime, String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date bTime = sdf.parse(beginTime);
            Date eTime = sdf.parse(endTime);
            if (bTime.after(eTime)) {
                Date t = bTime;
                bTime = eTime;
                eTime = t;
            }
            Calendar startCalendar = Calendar.getInstance();
            startCalendar.setTime(bTime);
            Calendar endCalendar = Calendar.getInstance();
            endCalendar.setTime(eTime);
            Calendar temp = Calendar.getInstance();
            temp.setTime(eTime);
            temp.add(Calendar.DATE, 1);

            int year = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
            int month = endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
            int intType = 0;
            if ((startCalendar.get(Calendar.DATE) == 1) && (temp.get(Calendar.DATE) == 1)) {
                intType = year * 12 + month + 1;
            } else if (((startCalendar.get(Calendar.DATE) != 1) && (temp.get(Calendar.DATE) == 1)) || ((startCalendar.get(Calendar.DATE) == 1) && (temp.get(Calendar.DATE) != 1))) {
                intType = year * 12 + month;
            } else {
                if((((year * 12) + month) - 1) >= 0){
                    intType = (year * 12 + month);
                }
            }
            return intType;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**.
     * 两个时间比较大小
     * @param strDate1 第一个时间
     * @param strDate2 第二个时间
     * @return 1:strDate1 大     -1:strDate2 大  0:相等
     */
    public static int compareDate(String strDate1, String strDate2) {
        DateFormat df = new SimpleDateFormat(CN_DATETIME_FORMAT);
        try {
            Date dt1 = df.parse(strDate1);
            Date dt2 = df.parse(strDate2);
            if (dt1.getTime() > dt2.getTime()) {
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                return -1;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }
    /**
     * 月份加减
     * @Title: getMONTH
     * @Description: 月份加减
     * @param iMonth 月份
     * @return 返回加减后的时间
     * @since v1.0.0
     */
    public static String getMONTH(int iMonth) {
        DateFormat df=new SimpleDateFormat(CN_DATETIME_FORMAT);
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, iMonth);
        return df.format(cal.getTime());
    }
    /**
     * @Title: main
     * @Description: 测试入口
     * @param args .
     * @since v1.0.9
     */
    public static void main(String[] args) {
        int i = compareDate("1995-11-12", "1999-12-11");
        System.out.println("i==" + i);
    }
}
